package com.wgy.blog.common;

import com.wgy.blog.common.enums.MessageEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @Author:I* @Data：2021/10/19 21:55
 */
@ApiModel(value = "返回格式实体", description = "用于规范格式")
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "数据格式代号",example = "成功为1，失败为-1")
    private Integer code;

    @ApiModelProperty(value = "回响提示")
    private String message;

    @ApiModelProperty(value = "影响条数或者查询到的数量")
    private Integer total;
    
    @ApiModelProperty(value = "返回的数据")
    private T data;

    public Result() {
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }



    public Result(T data) {
        this.code = MessageEnum.SUCCESS.getCode();
        this.message = MessageEnum.SUCCESS.getMessage();
        this.data = data;
    }

    public Result(T data, int total) {
        this.code = MessageEnum.SUCCESS.getCode();
        this.message = MessageEnum.SUCCESS.getMessage();
        this.data = data;
        this.total = total;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }
    public static <T> Result<T> success() {
        return new Result<>(MessageEnum.SUCCESS.getCode(), MessageEnum.SUCCESS.getMessage());
    }

    public static <T> Result<T> success(int total, T data) {
        return new Result<>(data, total);
    }


    public static <T> Result<T> success(String message, T data) {
        return new Result<>(MessageEnum.SUCCESS.getCode(), message, data);
    }


    public static <T> Result<T> success(String message) {
        return new Result<>(MessageEnum.SUCCESS.getCode(), message);
    }

    public static <T> Result<T> fail() {
        return new Result<>(MessageEnum.ERROR.getCode(), MessageEnum.ERROR.getMessage());
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(MessageEnum.ERROR.getCode(), message);
    }


    public static <T> Result<T> fail(MessageEnum messageEnum) {
        return new Result<>(messageEnum.getCode(), messageEnum.getMessage());
    }
    public static <T> Result<T> fail(MessageEnum messageEnum,String message) {
        return new Result<>(messageEnum.getCode(), message);
    }
    public static Result addFail() {
        return new Result<>(MessageEnum.ADD_ERROR.getCode(), MessageEnum.ADD_ERROR.getMessage());
    }
    public static  Result addSuccess() {
        return new Result(MessageEnum.ADD_SUCCESS.getCode(), MessageEnum.ADD_SUCCESS.getMessage());
    }
}
